<?php
class products extends MY_Model{
    public function __construct() {
        parent::__construct();
    }
    
    //insert product
    public function addProduct($fields=array())
    {   
        if(empty($fields)){
            return false;
        }
        $this->db->insert('products',$fields);
        return $this->db->insert_id();
    }
    
    //get product of category
    public function getProductCategory($cate_id= false,$limited=false)
    {
        $this->db->order_by('product_id','desc');
        if($cate_id){
            $this->db->where(array('cat_id'=>$cate_id,'valid'=>0));
        }
        if($limited)
            $this->db->limit($limited);
        $query = $this->db->get('products');
        return $query->result();
    }
    
    //get product
    public function getProduct($condition=array()){
        $this->db->order_by('product_id','desc');
        if(empty($condition)){
            $this->db->where(array('valid'=>0));
            $query = $this->db->get('products');
        }
        else
            $query = $this->db->get_where('products',$condition);
       
        if(isset($condition['product_id']))
            return $query->row();
        return $query->result();
    }
    //update product
    public function updateProduct($field=array(),$conditions =array())
    {
            $this->db->update('products',$field,$conditions);
            return $this->db->affected_rows();
    }
    
    //delete product
    public function deleteProduct($pro_id=false){
        if($pro_id){
            $this->db->where('product_id',$pro_id);
            $data = array('valid'=>1);
            $this->db->update('products',$data);
        }
        return $this->db->affected_rows(); 
    }
    
    
}
?>